<!DOCTYPE html>
<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Percy Doc Thrower</title>
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script>
        <script src="./lib/getQueryString.js"></script>
        <script src="./src/buildComponentDocBlock.js"></script>
        <script src="./src/extractParameters.js"></script>
        <script src="./src/findModule.js"></script>
        <script src="./src/findComponent.js"></script>
        <script src="./src/findComponentFunctions.js"></script>
        <script src="./src/getTypesList.js"></script>
        <script src="./src/parseComponentSource.js"></script>
    </head>
    <body>

        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
            <a class="navbar-brand" href="index.html">Percy - Component</a>
            <a class="navbar-brand" href="function.html" id="swapType">(swap to Function)</a>
            <div class="navbar-nav">
                <a class="nav-item nav-link" href="index.html?filter=all&show=all">All</a>
                <a class="nav-item nav-link" href="index.html?filter=all&show=done">Done</a>
                <a class="nav-item nav-link" href="index.html?filter=all&show=todo">Remaining</a>
                <a class="nav-item nav-link" href="../out/">Output</a>
            </div>
        </nav>

        <div class="container-fluid">

            <div class="row">
                <div class="col">

                    <h2>Source</h2>

                    <form>

                        <div class="form-group">
                            <label for="class">Namespace</label>

                            <div class="input-group">
                                <span class="input-group-btn">
                                    <button class="btn btn-secondary" type="button" id="saveClassButton">Save Class</button>
                                </span>
                                <input type="text" class="form-control" id="class" value="Phaser.">
                            </div>
                        </div>

                        <div class="alert alert-danger" id="sourceAlert" role="alert" style="display: none;">Alert!</div>

                        <div class="form-group">
                            <textarea id="input" class="form-control" rows="33" style="font-family: Consolas" spellcheck="false"></textarea>
                        </div>
                        <div class="form-group">
                            <button type="button" id="parseButton" class="btn btn-primary">Parse</button>
                            <button type="button" id="copyButton" class="btn btn-warning mx-4">Copy -&gt;</button>
                        </div>

                    </form>

                </div>
                <div class="col">

                    <h2>Output</h2>

                    <form>

                        <div class="form-group">
                            <label for="filename">Filename</label>
                            <input type="text" id="filename" class="form-control" />
                        </div>

                        <div class="alert alert-success" id="saveAlert" role="alert" style="display: none; position: absolute; top: 2px; right: 2px">Saved!</div>

                        <div class="form-group">
                            <textarea id="output" class="form-control" rows="33" style="font-family: Consolas" spellcheck="false"></textarea>
                        </div>
                        <div class="form-group">
                            <button type="button" id="saveButton" class="btn btn-primary">Save File</button>
                            <button type="button" id="markButton" class="btn btn-success mx-4">Mark Done</button>
                            <button type="button" id="nextButton" class="btn btn-danger">Next File</button>
                        </div>

                    </form>

                </div>
            </div>
        </div>

        <!-- Parameters Modal -->
        <div class="modal fade" id="paramsModal" tabindex="-1" role="dialog">
            <div class="modal-dialog modal-lg" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="paramsModalLabel">Set Parameter Types</h5>
                        <button type="button" class="close" data-dismiss="modal">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body" id="paramsContainer"></div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary" id="updateButton">Save Params</button>
                    </div>
                </div>
            </div>
        </div>

        <script type="text/javascript">

        //  Set-up the handlers

        $(document).ready(function () {

            $('#swapType').attr('href', 'function.html' + location.search);

            $('#nextButton').hide();

            var filename = getQueryString('f');

            $('#filename').val(filename);

            $('#input').load('/phaser/src/' + filename, function () {

                //  Success loading the source

                var namespace = filename.split('\\');

                namespace.pop();
                namespace.pop();

                namespace.forEach(function (value, index, array) {

                    array[index] = value.charAt(0).toUpperCase() + value.substr(1);

                });

                $('#class').val('Phaser.' + namespace.join('.'));

                var c = localStorage.getItem('className');

                if (c)
                {
                    $('#class').val(c);
                }

                //  Set-up the buttons

                $('#parseButton').click(function() {

                    parseSource();

                });

                $('#saveButton').click(function() {

                    var data = {
                        filename: $('#filename').val(),
                        src: $('#output').val()
                    };

                    //  Save to SQL
                    $.post('save.php', data, function (result) {

                        if (result == 'OK')
                        {
                            $('#saveAlert').show();
                        }

                    });

                });

                $('#copyButton').click(function() {

                    $('#output').val($('#input').val());

                });

                $('#saveClassButton').click(function() {

                    var c = $('#class').val();

                    localStorage.setItem('className', c);

                });

                $('#markButton').click(function() {

                    var data = {
                        filename: $('#filename').val()
                    };

                    //  Save to SQL
                    $.post('markdone.php', data, function (result) {

                        if (result == 'OK')
                        {
                            $('#saveAlert').show();
                        }

                    });

                });

                var data = {
                    filename: $('#filename').val()
                };

                $.post('getnext.php', data, function (result) {

                    if (result !== '')
                    {
                        console.log('Next file:', result);

                        $('#nextButton').show();

                        $('#nextButton').click(function() {

                            window.location.href = 'component.html?f=' + result;

                        });
                    }

                });

            });

        });

        </script>

    </body>
</html>